/**/

#include "wimp.h"
#include "wimpt.h"
#include "win.h"
#include "event.h"
#include "baricon.h"
#include "werr.h"
#include "bbc.h" 
#include "coords.h" 
#include "resspr.h"
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "polysaw.h"

static void  set_up_unitpic_perm(int p, int *spr);



void redraw_board(wimp_w handle)
{
int               i, p, row, col, g;
int               minr, minc, maxr, maxc;
int               more;
wimp_redrawstr    r;
wimp_box          wrkbox;
wimp_box          pcebox;

coords_pointstr   pt;
char              name[12];
sprite_id         sid;
int               unitpic_no[16]; 
sprite_factors    sprscale;
sprite_pixtrans   sprcols[16];



sid.tag = sprite_id_name;
sid.s.name = name;
r.w = handle;

wimpt_noerr(wimp_redraw_wind(&r, &more));
while (more)
  {
  wrkbox = r.g;
  coords_box_toworkarea(&wrkbox, (coords_cvtstr *)&r.box);
  minc = wrkbox.x0 / (2*HS);
  maxr = (-wrkbox.y0) / (2*HS);
  maxc = wrkbox.x1 / (2*HS);
  minr = (-wrkbox.y1) / (2*HS);

  for (row=minr; row<=maxr; row++)
    {
    for (col=minc; col<=maxc; col++)
      {
      g = grid[row][col];
      if (g & SQUARE_BIT)
        {
        sprintf(name, "0");
        pt.x = 2*HS*col;
        pt.y = -2*HS*row - 2*HS;
        coords_point_toscreen(&pt, (coords_cvtstr *)&r.box);
        wimpt_noerr(wimp_readpixtrans(resspr_area(), &sid, &sprscale, sprcols));
        wimpt_noerr(sprite_put_scaled(resspr_area(), &sid, 0, pt.x, pt.y, &sprscale, sprcols));
        }
      else if (g & HOLE_BIT)
        {
        sprintf(name, "hole");
        pt.x = 2*HS*col;
        pt.y = -2*HS*row - 2*HS;
        coords_point_toscreen(&pt, (coords_cvtstr *)&r.box);
        wimpt_noerr(wimp_readpixtrans(resspr_area(), &sid, &sprscale, sprcols));
        wimpt_noerr(sprite_put_scaled(resspr_area(), &sid, 0, pt.x, pt.y, &sprscale, sprcols));
        }
      } 
    }

  for (p=0; p<nofpieces; p++)
    {
    pcebox.x0 = piece[p].cog.x + piece[p].box.x0;
    pcebox.y0 = piece[p].cog.y + piece[p].box.y0;
    pcebox.x1 = piece[p].cog.x + piece[p].box.x1;
    pcebox.y1 = piece[p].cog.y + piece[p].box.y1;
    if (!coords_boxesoverlap(&pcebox, &wrkbox)) 
       continue;

    set_up_unitpic_perm(p, unitpic_no);

    /* plot the piece */  
    for (i=0; i < piece[p].nofunits; i++)
      {
      sprintf(name, "%0x",  unitpic_no[ piece[p].unitpic[i] ] );      
      pt.x = piece[p].cog.x + piece[p].units[i].x - HS;
      pt.y = piece[p].cog.y + piece[p].units[i].y - HS;
      coords_point_toscreen(&pt, (coords_cvtstr *)&r.box);
      wimpt_noerr(wimp_readpixtrans(resspr_area(), &sid, &sprscale, sprcols));
      wimpt_noerr(sprite_put_scaled(resspr_area(), &sid, 0, pt.x, pt.y, &sprscale, sprcols));
      } 

    } /* next piece */

    wimp_get_rectangle(&r, &more);
  } 
}


void  set_up_unitpic_perm(int p, int *spr)
{
int  rots, n, flip;

flip = piece[p].rotflipstate & 1;
rots = piece[p].rotflipstate >> 1;
for (n=0; n<16; n++)
    {
    if (flip) 
        spr[n] = (n&5) | (n&2)<<2 | (n&8)>>2;
    else
        spr[n] = n;
    }
 for (n=0; n<16; n++)
    spr[n] = ( spr[n]<<rots | spr[n]>>(4-rots) ) & 15;
}






